const geocoder = new AMap.Geocoder({
  radius: 1000,
  extensions: "all",
  batch:true
})

const getAddress = lnglats => new Promise(async (resolve, reject)=>{
	 ;
	let list = lnglats?lnglats.filter(v=>v[0]):[];
	if(list.length){
		let res = [];
		for(let i = 0;i<lnglats.length;i+=20){
			let _nres = await new Promise((resolve, reject)=>{
				let a = i;
				geocoder.getAddress(lnglats.slice(a,a+20).filter(v=>v[0]), function(status, result) {
				  if (status === 'complete' && result.info === 'OK') {
		        let regeocodes = [], k = 0,_res = {};
		        lnglats.slice(a,a+20).forEach(([lng,lat]) => {
		        	if(lng){
        				let _r = result.regeocodes[k++];
        				regeocodes.push(_r);
		        	}else{
		        		regeocodes.push(null);
		        	}

		        });
				    resolve(regeocodes);
				  }else{
				  	resolve([]);
				  }
				})
			})
			 ;
			if(!_nres.length){
				_nres = [];
				lnglats.slice(i,i+20).forEach(()=>{
					_nres.push(null);
				});
			}
				
			res.push(..._nres);
		}
		resolve(res);
		
	}else{
		resolve([]);
	}
})

const getLocation = str => new Promise((resolve, reject)=>{
	if(str.length){
		geocoder.getLocation(str, function(status, result) {
		  if (status === 'complete' && result.info === 'OK') {
		    resolve(result.geocodes[0].location);
		  }else{
		  	resolve(null);
		  }
		})
	}else{
		resolve([]);
	}
})
export default {getAddress,getLocation}
export {getAddress,getLocation}
